package com.template.mongo.utils;

import java.io.IOException;
//import java.io.UnsupportedEncodingException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

public class PagingTag extends TagSupport {
	static Logger logger = Logger.getLogger(PagingTag.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = -3479891350802464345L;
	private PagingInfo pagingInf;
	private String searchUrl;
	private String curPageParam;
	private String searchValueParam;
	private String imgFirst;
	private String imgPrev;
	private String imgNext;
	private String imgLast;

	@Override
	public int doStartTag() throws JspException {
		
		try {
			JspWriter out = pageContext.getOut();
			String strTrangDau = new String("\u0110\u1EA7u");
			String strTrangTruoc = new String("Tr\u01B0\u1EDBc");
			String strTrangSau = new String("Sau");
			String strTrangCuoi = new String("Cu\u1ED1i");
			
			if (pagingInf.getTotalPage() > 1) {
				try {

					if (pagingInf.getCurrentPage() != 1) {
//						out.write("<button class='bPage' value='1' title=\""+ strTrangDau +"\" ><span>"+ strTrangDau +"</span></button>");
						out.write("<li><a class='bPage' title='"+ strTrangDau +"' href='#1'><img width='16' height='16' src='" + imgFirst + "'> "+ strTrangDau +"</a></li>");
					}

					if (pagingInf.getCurrentPage() <= pagingInf.getTotalPage()
							&& pagingInf.getCurrentPage() != 1) {
//						out.write("<button class='bPage' value="
//								+ (pagingInf.getCurrentPage() - 1)
//								+ " title=\""
//								+ strTrangTruoc
//								+ "\" ><span>"+ strTrangTruoc+"</span></button>");
						out.write("<li><a class='bPage' title='"+ strTrangTruoc +"' href='#"
								+ (pagingInf.getCurrentPage() - 1) 
								+ "'><img width='16' height='16' src='" + imgPrev + "'> "
								+ strTrangTruoc +"</a></li>");
					}

					for (int i = pagingInf.getBegin(); i <= pagingInf.getEnd(); i++) {
						if (i == pagingInf.getCurrentPage()) {
//							out.write("<button class=\"active\"><span>" + i
//									+ "</span></button>");
							out.write("<li><a class='current' title='Trang "+ i +"' href='#"+ i 
									+"'><b>"+ i +"</b></a></li>");
							
						} else {
//							out.write("<button class='bPage' value="+ i	+ " title='Trang " + i + "'><span>" + i + "</span></button>");
							out.write("<li><a class='bPage' title='Trang " + i + "' href='#"+ i 
									+"'><b>"+ i +"</b></a></li>");
						}
					}

					if (pagingInf.getCurrentPage() < pagingInf.getTotalPage()) {
//						out.write("<button class='bPage' value="+ (pagingInf.getCurrentPage() + 1) + " title=\"" + strTrangSau + "\"><span>" + strTrangSau + "</span></button>");
						out.write("<li><a class='bPage' title='"+ strTrangSau +"' href='#"+ (pagingInf.getCurrentPage() + 1) +"'>"+ strTrangSau +" <img width='16' height='16' src='" + imgNext + "'></a></li>");
					}

					if (pagingInf.getCurrentPage() != pagingInf.getTotalPage()) {
//						out.write("<button class='bPage' value="
//								+ pagingInf.getTotalPage()
//								+ " title=\""
//								+ strTrangCuoi
//								+ "\"><span>"
//								+ strTrangCuoi
//								+ "</span></button>");
						out.write("<li><a class='bPage' title='"+ strTrangCuoi +"' href='#"
								+ pagingInf.getTotalPage() +"'>"
								+ strTrangCuoi +" <img width='16' height='16' src='" + imgLast + "'></a></li>");
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (Exception e1) {
			logger.error(e1.getMessage(), e1);
		}		

		return SKIP_BODY;
	}

	/**
	 * @param pagingInf
	 *            the pagingInf to set
	 */
	public void setPagingInf(PagingInfo pagingInf) {
		this.pagingInf = pagingInf;
	}

	/**
	 * @return the pagingInf
	 */
	public PagingInfo getPagingInf() {
		return pagingInf;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public void setSearchUrl(String url) {
		this.searchUrl = url;
	}

	/**
	 * @return the url
	 */
	public String getSearchUrl() {
		return searchUrl;
	}

	/**
	 * @param curPageParam
	 *            the curPageParam to set
	 */
	public void setCurPageParam(String curPageParam) {
		this.curPageParam = curPageParam;
	}

	/**
	 * @return the curPageParam
	 */
	public String getCurPageParam() {
		return curPageParam;
	}

	/**
	 * @param searchValueParam
	 *            the searchValueParam to set
	 */
	public void setSearchValueParam(String searchValueParam) {
		this.searchValueParam = searchValueParam;
	}

	/**
	 * @return the searchValueParam
	 */
	public String getSearchValueParam() {
		return searchValueParam;
	}

	/**
	 * @return the imgFirst
	 */
	public String getImgFirst() {
		return imgFirst;
	}

	/**
	 * @param imgFirst the imgFirst to set
	 */
	public void setImgFirst(String imgFirst) {
		this.imgFirst = imgFirst;
	}

	/**
	 * @return the imgPrev
	 */
	public String getImgPrev() {
		return imgPrev;
	}

	/**
	 * @param imgPrev the imgPrev to set
	 */
	public void setImgPrev(String imgPrev) {
		this.imgPrev = imgPrev;
	}

	/**
	 * @return the imgNext
	 */
	public String getImgNext() {
		return imgNext;
	}

	/**
	 * @param imgNext the imgNext to set
	 */
	public void setImgNext(String imgNext) {
		this.imgNext = imgNext;
	}

	/**
	 * @return the imgLast
	 */
	public String getImgLast() {
		return imgLast;
	}

	/**
	 * @param imgLast the imgLast to set
	 */
	public void setImgLast(String imgLast) {
		this.imgLast = imgLast;
	}
}
